
		
************************************************************************************************************************
* Natural Experiment 
************************************************************************************************************************

* Note: to get from the main data set to this one, this is the line of code: keep if ~missing( under_order) & tract_count>1  & elem_school_count >1 & year_lifted_stan_num>=1995 & ncesid~=2201410 & ncesid~=2201170  


use natural_experiment_data, clear
tsset ncesid year, yearly
sort ncesid year


sort STATE year
egen state_year=group(STATE year) if year>=1995


tab ncesid, g(ncesid_)
tab state_year, g(state_year_)
* this is to create a list that can be used both with xtreg2 (cannot contain factor variables) and does not contain any ncesid's that are dropped because of colinearity. The model would run the same with i.ncesid but could not run margins afterward
global ncesid_list ncesid_1 ncesid_2 ncesid_5 ncesid_6 ncesid_7 ncesid_8    ncesid_9 ncesid_10 ncesid_11 ncesid_12 ncesid_13 ncesid_14  ncesid_15 ncesid_16 ncesid_17 ncesid_18 ncesid_19                      ncesid_21 ncesid_22 ncesid_23 ncesid_24 ncesid_26                      ncesid_27 ncesid_28 ncesid_30 ncesid_31 ncesid_32                      ncesid_34 ncesid_35 ncesid_36 ncesid_38 ncesid_39                      ncesid_40 ncesid_41 ncesid_42 ncesid_43 ncesid_44                      ncesid_45 ncesid_46 ncesid_47 ncesid_48 ncesid_49                     ncesid_50 ncesid_51 ncesid_52 ncesid_53 ncesid_54                      ncesid_55 ncesid_57 ncesid_60 ncesid_61 ncesid_62                      ncesid_63 ncesid_64 ncesid_65 ncesid_66 ncesid_68                      ncesid_69 ncesid_70 ncesid_71 ncesid_73 ncesid_75                      ncesid_76 ncesid_77 ncesid_78 ncesid_79 ncesid_81                      ncesid_84 ncesid_85 ncesid_86 ncesid_87 ncesid_88                      ncesid_90 ncesid_91 ncesid_92 ncesid_96 ncesid_97                      ncesid_98 ncesid_99 ncesid_101 ncesid_102 ncesid_106                      ncesid_107 ncesid_109 ncesid_111 ncesid_112 ncesid_113                      ncesid_114 ncesid_116 ncesid_118 ncesid_119 ncesid_120                      ncesid_121 ncesid_123 ncesid_124 ncesid_126 ncesid_128                      ncesid_129 ncesid_130 ncesid_131 ncesid_132 ncesid_133                      ncesid_136 ncesid_138 ncesid_139 ncesid_140 ncesid_141                      ncesid_143 ncesid_144 ncesid_145 ncesid_146 ncesid_147                      ncesid_149 ncesid_150 ncesid_151 ncesid_152 ncesid_153                      ncesid_154 ncesid_155 ncesid_156 ncesid_157 ncesid_158                      ncesid_159 ncesid_160 ncesid_161 ncesid_162 ncesid_165                      ncesid_166 ncesid_167 ncesid_168 ncesid_169 ncesid_170                      ncesid_171 ncesid_172 ncesid_173 ncesid_174 ncesid_175                      ncesid_176 ncesid_177 ncesid_178 ncesid_180 ncesid_181                      ncesid_182 ncesid_183 ncesid_184 ncesid_185 ncesid_186                      ncesid_187 ncesid_188 ncesid_190 ncesid_192 ncesid_195                      ncesid_196 ncesid_197 ncesid_198 ncesid_199 ncesid_200                      ncesid_202 ncesid_203 ncesid_204 ncesid_205 ncesid_206                      ncesid_207 ncesid_208 ncesid_209 ncesid_210 ncesid_213                      ncesid_214 ncesid_215 ncesid_217 ncesid_218 ncesid_219                      ncesid_220 ncesid_221 ncesid_223 ncesid_224 ncesid_225                      ncesid_226 ncesid_227 ncesid_228 ncesid_229 ncesid_230                      ncesid_233 ncesid_234 ncesid_235 ncesid_236 ncesid_237                      ncesid_239 ncesid_241 ncesid_242 ncesid_244 ncesid_245                      ncesid_246 ncesid_247 ncesid_250 ncesid_253 ncesid_254                      ncesid_257 ncesid_258 ncesid_259 ncesid_260 ncesid_261                      ncesid_262 ncesid_263 ncesid_264 ncesid_266 ncesid_267                      ncesid_268 ncesid_269 ncesid_270 ncesid_271 ncesid_273                      ncesid_274 ncesid_277 ncesid_278 ncesid_279 ncesid_280                      ncesid_281 ncesid_282 ncesid_283 ncesid_285 ncesid_286                      ncesid_287 ncesid_288 ncesid_289 ncesid_290 ncesid_292                      ncesid_293 ncesid_294 ncesid_295 ncesid_296 ncesid_297                      ncesid_298 ncesid_299 ncesid_300 ncesid_301 ncesid_302                      ncesid_303 ncesid_304 ncesid_305 ncesid_306 ncesid_307                      ncesid_308 ncesid_309 ncesid_310 ncesid_311 ncesid_312                      ncesid_313 ncesid_314 ncesid_316 ncesid_318 ncesid_320                      ncesid_321 ncesid_322 ncesid_323 ncesid_324 ncesid_325                      ncesid_326 ncesid_327 ncesid_328 ncesid_329 ncesid_330                      ncesid_331 ncesid_332 ncesid_333 ncesid_334 ncesid_335                      ncesid_336 ncesid_337 ncesid_338 ncesid_339 ncesid_340                      ncesid_341 ncesid_342 ncesid_343 ncesid_344 ncesid_345                      ncesid_346 ncesid_347 ncesid_348 ncesid_349 ncesid_350                      ncesid_351 ncesid_352 ncesid_353 ncesid_354 ncesid_355                      ncesid_357 ncesid_358 ncesid_360 ncesid_361 ncesid_362                      ncesid_363 ncesid_364 ncesid_365 ncesid_366 ncesid_367                      ncesid_368 ncesid_370 ncesid_371 ncesid_372 ncesid_373                      ncesid_374 ncesid_376 ncesid_377 ncesid_378 ncesid_379                      ncesid_381 ncesid_383 ncesid_384 ncesid_385 ncesid_386                      ncesid_388 ncesid_389 ncesid_390 ncesid_392 ncesid_393                      ncesid_394 ncesid_395 ncesid_396 ncesid_397 ncesid_398                      ncesid_399 ncesid_400 ncesid_401 ncesid_402 ncesid_403                      ncesid_405 ncesid_406 ncesid_407 ncesid_410 ncesid_411                      ncesid_412 ncesid_413 ncesid_414 ncesid_415 ncesid_416                     ncesid_417 ncesid_418 ncesid_420 ncesid_421 ncesid_422                     ncesid_423 ncesid_424 ncesid_425 ncesid_426 ncesid_427                      ncesid_428 ncesid_430 ncesid_431 ncesid_432 ncesid_433                      ncesid_434 ncesid_435 ncesid_436 ncesid_437 ncesid_438                      ncesid_443 ncesid_444 ncesid_445 ncesid_447 ncesid_448                      ncesid_449 ncesid_450 ncesid_451 ncesid_452 ncesid_453                      ncesid_454 ncesid_455 ncesid_456 ncesid_458 ncesid_459                      ncesid_460 ncesid_461 ncesid_462 ncesid_465 ncesid_466                      ncesid_467 ncesid_468 ncesid_469 ncesid_470 ncesid_471                      ncesid_472 ncesid_473 ncesid_474 ncesid_475 ncesid_476                      ncesid_477 ncesid_478 ncesid_479 ncesid_480 ncesid_482                      ncesid_483 ncesid_484 ncesid_485 ncesid_486 ncesid_488                      ncesid_489 ncesid_490 ncesid_491 ncesid_492 ncesid_493                      ncesid_494 ncesid_495 ncesid_496 ncesid_497 ncesid_499                      ncesid_503 ncesid_504 ncesid_505 ncesid_506 ncesid_508                      ncesid_510 ncesid_511 ncesid_512 ncesid_513 ncesid_514                     ncesid_515 ncesid_516 ncesid_517 ncesid_518 ncesid_519                      ncesid_520 ncesid_521 ncesid_522 ncesid_523 ncesid_524                      ncesid_525 ncesid_526 ncesid_527 ncesid_528 ncesid_529                      ncesid_530 ncesid_532 ncesid_533 ncesid_535 ncesid_536                      ncesid_537 ncesid_539 ncesid_540 ncesid_541 ncesid_542                      ncesid_546 ncesid_548 ncesid_549 ncesid_550 ncesid_552                      ncesid_553 ncesid_554 ncesid_555 ncesid_556 ncesid_558                      ncesid_559 ncesid_561 ncesid_562 ncesid_563 ncesid_565                      ncesid_566 ncesid_567 ncesid_568 ncesid_571 ncesid_572                      ncesid_573 ncesid_574 ncesid_575 ncesid_577 ncesid_578                      ncesid_579 ncesid_580 ncesid_581 ncesid_582 ncesid_584                      ncesid_585 ncesid_586 ncesid_587 ncesid_589 ncesid_590                     ncesid_595 ncesid_598 ncesid_599 ncesid_600 ncesid_601                      ncesid_602 ncesid_604 ncesid_605 ncesid_606 ncesid_607                      ncesid_608 ncesid_611 ncesid_612 ncesid_613 ncesid_614                     ncesid_615 ncesid_616 ncesid_617 ncesid_618 ncesid_620                      ncesid_621 ncesid_624 ncesid_626 ncesid_627 ncesid_628                      ncesid_630 ncesid_631 ncesid_633 ncesid_636 ncesid_637                      ncesid_639 ncesid_642 ncesid_643 ncesid_644 ncesid_645                      ncesid_646 ncesid_647 ncesid_648 ncesid_649 ncesid_652                     ncesid_653 ncesid_654 ncesid_655 ncesid_656 ncesid_657                      ncesid_658 ncesid_659 ncesid_660


set matsize 1000



* generate what districts should be included in the sample 
xtivreg per_pupil_local_thous (hindex_wb_schoollev_ipo=years_overturn_stan t_stan)   c.per_black_ipo per_hispanic_ipo change_black_5years change_hispanic_5years elem_school_count $indep  i.ncesid if tract_count>1  & elem_school_count>1 & year_lifted_stan_num>=1995 & ncesid~=2201410 & ncesid~=2201170   , i(state_year) fe first
gen sample_inst=e(sample)


*************************************************************************
 *  Create Table 2 and Table B.4 (created in data editor- scroll to end of the data sheet)
*************************************************************************
 
 gen var_inst=" "
 
 gen mean_1995_nooverturn=.
 gen std_dev_1995_nooverturn=.
 gen n_1995_nooverturn=.
 
 gen mean_1995_overturn=.
 gen std_dev_1995_overturn=.
 gen n_1995_overturn=.
 gen t_test_result=.
 
 
 
 gen mean_onebefore_overturn=.
 gen std_dev_onebefore_overturn=.
 gen n_onebefore_overturn=.
 
 gen mean_oneafter_overturn=.
 gen std_dev_oneafter_overturn=.
 gen n_oneafter_overturn=.
 
  gen mean_oneafter_overturn5=.
 gen std_dev_oneafter_overturn5=.
 gen n_oneafter_overturn5=.
 
 gen t_test_result_before=.
 
  gen t_test_result_before5=.
 
 gen before_after=1 if t_stan==1
 replace before_after=0 if t_stan==-1
 
 gen before_after5=1 if t_stan==5
 replace before_after5=0 if t_stan==-1
 
 local n=1
 foreach var in per_pupil_local_thous per_pupil_state per_pupil_fed TheileH_wb_ipo dissim_wb_ipo hindex_wb_schoollev per_black_ipo per_hispanic_ipo change_black_5years change_hispanic_5years  median_hh_income_thous log_pop bach pres_dem V33    { 
 
     replace var="`var'" if _n==`n'
 
	 sum `var' if year==1995 & black_count>0 & tract_count>1 & sample_inst==1 & still_order==0
	 replace mean_1995_nooverturn=r(mean) if _n==`n'
	 replace std_dev_1995_nooverturn=r(sd) if _n==`n'
	 replace n_1995_nooverturn=r(N) if _n==`n'
	 
	 sum `var' if year==1995  & black_count>0 & tract_count>1 & sample_inst==1 & still_order==1
	 replace mean_1995_overturn=r(mean) if _n==`n'
	 replace std_dev_1995_overturn=r(sd) if _n==`n'
	 replace n_1995_overturn=r(N) if _n==`n'
	 
	 ttest `var' if year==1995 & black_count>0 & tract_count>1 & sample_inst==1, by(still_order)
	 replace t_test_result=r(p) if _n==`n'
	 
	 
	 * Compare one year before and one year after dismissal 
	 sum `var' if  black_count>0 & tract_count>1 & t_stan==-1 & sample_inst==1 
	 replace mean_onebefore_overturn=r(mean) if _n==`n'
	 replace std_dev_onebefore_overturn=r(sd) if _n==`n'
	 replace n_onebefore_overturn=r(N) if _n==`n'
	 
	 sum `var' if black_count>0 & tract_count>1 & t_stan==1 & sample_inst==1 
	 replace mean_oneafter_overturn=r(mean) if _n==`n'
	 replace std_dev_oneafter_overturn=r(sd) if _n==`n'
	 replace n_oneafter_overturn=r(N) if _n==`n'
	 
	 	ttest `var' if   black_count>0 & tract_count>1 & sample_inst==1, by(before_after)
	  replace t_test_result_before=r(p) if _n==`n'

	 
	 	 sum `var' if black_count>0 & tract_count>1 & t_stan==5 & sample_inst==1
	 replace mean_oneafter_overturn5=r(mean) if _n==`n'
	 replace std_dev_oneafter_overturn5=r(sd) if _n==`n'
	 replace n_oneafter_overturn5=r(N) if _n==`n'
	

	  ttest `var' if   black_count>0 & tract_count>1 & sample_inst==1, by(before_after5)
	  replace t_test_result_before5=r(p) if _n==`n'
	  
	 
 local n=`n'+1
 
 }




********************************
* Table B.5 and Figure 4 $
********************************

eststo court1: xtreg hindex_wb_schoollev_ipo years_overturn_stan t_stan c.per_black_ipo per_hispanic_ipo change_black_5years change_hispanic_5years elem_school_count $indep  $ncesid_list if tract_count>1  & elem_school_count >1 & year_lifted_stan_num>=1995 & ncesid~=2201410 & ncesid~=2201170   , i(state_year) fe 
* Graph 
margins, at(  years_overturn_stan=(0(1)10))
marginsplot, legend(order(3 "Segregation")) recast(line) recastci(rarea) scheme(s1mono) title("First Stage: Predicting Segregation") ytitle("School Segregation") xtitle("Years Overturned")   
graph export "graph_court1.pdf", as(pdf) replace


* As an instrumental variable 
eststo court2: xtivreg per_pupil_local_thous (hindex_wb_schoollev_ipo=years_overturn_stan t_stan)  per_black_ipo per_hispanic_ipo change_black_5years change_hispanic_5years elem_school_count $indep  $ncesid_list if tract_count>1  & elem_school_count>1 & year_lifted_stan_num>=1995 & ncesid~=2201410 & ncesid~=2201170   , i(state_year) fe first 
margins, at(  hindex_wb_schoollev_ipo=(0(.1).7))
marginsplot, legend(order(3 "Segregation")) recast(line) recastci(rarea) scheme(s1mono) title("Second Stage: Predicting Per Pupil Local Revenue") ytitle("Per Pupil Local Revenue (in thous)") xtitle("WB School Segregation")
graph export "graph_court2.pdf", as(pdf) replace

margins, at(hindex_wb_schoollev_ipo=.0206407)
margins, at(hindex_wb_schoollev_ipo= .1973105 )


eststo court3: xtivreg percent_public (hindex_wb_schoollev_ipo=years_overturn_stan t_stan)   c.per_black_ipo per_hispanic_ipo change_black_5years change_hispanic_5years elem_school_count $indep $ncesid_list if tract_count>1  & elem_school_count>1 & year_lifted_stan_num>=1995 & ncesid~=2201410 & ncesid~=2201170   , i(state_year) fe first
sum hindex_wb_schoollev_ipo, detail
margins, at(hindex_wb_schoollev_ipo=.0206407)
margins, at(hindex_wb_schoollev_ipo= .1973105 )

esttab court1 court2 court3 using "instrumental.tex", title(Regression table\label{instrumental}) replace  nostar cells(b(star fmt(3)) se(par fmt(3)))    width(0.8\hsize)  ///
drop($ncesid_list)





